/**
 * Copyright &copy; 2012-2016 <a href="https://istep.lczyfz.com">Istep</a> All rights reserved.
 */
package com.lczyfz.istep.modules.scene.web;

import com.lczyfz.istep.common.config.Global;
import com.lczyfz.istep.common.persistence.Page;
import com.lczyfz.istep.common.utils.StringUtils;
import com.lczyfz.istep.common.web.BaseController;
import com.lczyfz.istep.modules.scene.entity.ScenePort;
import com.lczyfz.istep.modules.scene.entity.SceneSubnet;
import com.lczyfz.istep.modules.scene.service.ScenePortService;
import com.lczyfz.istep.modules.scene.service.SceneSubnetService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 端口管理Controller
 * @author dh
 * @version 2017-04-17
 */
@Controller
@RequestMapping(value = "${adminPath}/scene/scenePort")
public class ScenePortController extends BaseController {

	@Autowired
	private ScenePortService scenePortService;
	@Autowired
	private SceneSubnetService sceneSubnetService;
	@ModelAttribute
	public ScenePort get(@RequestParam(required=false) String id) {
		ScenePort entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = scenePortService.get(id);
		}
		if (entity == null){
			entity = new ScenePort();
		}
		return entity;
	}
	
	@RequiresPermissions("scene:scenePort:view")
	@RequestMapping(value = {"list", ""})
	public String list(ScenePort scenePort, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ScenePort> page = scenePortService.findPage(new Page<ScenePort>(request, response), scenePort); 
		model.addAttribute("page", page);
		return "modules/scene/scenePortList";
	}

	@RequiresPermissions("scene:scenePort:view")
	@RequestMapping(value = "form")
	public String form(ScenePort scenePort, Model model) {
		SceneSubnet sceneSubnet=new SceneSubnet();
		List<SceneSubnet> sceneSubnetList=sceneSubnetService.findList(sceneSubnet);
		model.addAttribute("subnetList", sceneSubnetList);
		model.addAttribute("scenePort", scenePort);
		return "modules/scene/scenePortForm";
	}

	@RequiresPermissions("scene:scenePort:edit")
	@RequestMapping(value = "save")
	public String save(ScenePort scenePort, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, scenePort)){
			return form(scenePort, model);
		}
		logger.debug("scenePort=== [{}]",scenePort.getName()+" "+scenePort.getNetworkId()+" "+scenePort.getFixedIp()+" ");

//		try{
//			PortImpl portImpl=new PortImpl();
//			Port port =new Port();
//			port.setPortName(scenePort.getName());
//			port.setPortNetworkId(scenePort.getNetworkId());
//			JSONArray fixedIps = new JSONArray();
//			JSONObject fixedIp = new JSONObject();
//			fixedIp.put("ip_address", scenePort.getFixedIp());
//			fixedIps.add(0, fixedIp);
//			port.setPortFixedIp(fixedIps);
//			Port newPort =portImpl.createPort(port);
//			scenePort.setIsNewRecord(true);
//			scenePort.setId(newPort.getPortId());
//			scenePortService.save(scenePort);
//			addMessage(redirectAttributes, "保存端口成功");
//		}catch (RuntimeException e){
//			addMessage(redirectAttributes, "保存端口失败");
//
//		}
		scenePortService.save(scenePort);
		addMessage(redirectAttributes, "保存端口成功");
		return "redirect:"+Global.getAdminPath()+"/scene/scenePort/?repage";
	}
	
	@RequiresPermissions("scene:scenePort:edit")
	@RequestMapping(value = "delete")
	public String delete(ScenePort scenePort, RedirectAttributes redirectAttributes) {
		scenePortService.delete(scenePort);
		addMessage(redirectAttributes, "删除端口成功");
		return "redirect:"+Global.getAdminPath()+"/scene/scenePort/?repage";
	}

}